var mobId, mob, stats, modified, MaxRandom, setupTask, ostats, victor, luck;
var 清怪 = true;
var 刷新地图 = false;
var 复活次数 = 11;
var 副本名称 = "黑魔法师[困难]";
var BOSS_1_id = 8880500;
var BOSS_1_血量倍率 = 1;
var BOSS_2_id = 8880501;
var BOSS_2_血量倍率 = 1;
var BOSS血条_1_id = 8880505;
var BOSS_3_id = 8880502;
var BOSS_3_血量倍率 = 1;
var BOSS_4_id = 8880503;
var BOSS_4_血量倍率 = 1;
var BOSS_5_id = 8880504;
var BOSS_5_血量倍率 = 1;
var BOSS血条_2_id = 8880519;
var 副本时限 = 1000 * 60 * 60;
var 实例地图 = [450013000, 450013100, 450013200, 450013300, 450013400, 450013500, 450013600, 450013700];
var 准备地图 = 450012500;
var 退场倒计时 = 180;
var 哭墙倒计时 = 20;
var 哭墙CD = 10;

function init() {}

function setup(a, c) {
    em.setProperty("state", "1");
    em.setProperty("leader", "true");
    var k = em.newInstance(副本名称);
    for (var f = 0; f < 实例地图.length; f++) {
        var b = k.setInstanceMap(实例地图[f]);
        b.resetPQ(a);
        b.resetFully();
        b.killAllMonsters(清怪);
        b.respawn(刷新地图)
    }
    k.startEventTimer(副本时限);
    k.setProperty("FBtime", 副本时限);
    k.setProperty("FBname", 副本名称);
    k.setProperty("strMap", 准备地图);
    var h = em.getMonster(BOSS_1_id).getMobMaxHp() * BOSS_1_血量倍率;
    var d = em.getMonster(BOSS_2_id).getMobMaxHp() * BOSS_2_血量倍率;
    var l = em.getMonster(BOSS_3_id).getMobMaxHp() * BOSS_3_血量倍率;
    var j = em.getMonster(BOSS_4_id).getMobMaxHp() * BOSS_4_血量倍率;
    var e = em.getMonster(BOSS_5_id).getMobMaxHp() * BOSS_5_血量倍率;
    k.setProperty("BOSShp", h + d + l + j + e);
    k.setProperty("whiteKnight", "0");
    k.setProperty("blackKnight", "0");
    k.schedule("wailingwall", 哭墙倒计时 * 1000);
    mobId = BOSS_1_id;
    mob = em.getMonster(mobId);
    mob.setOverrideStats();
    mob.getChangedStats().hp = h;
    mob.getChangedStats().mp = 99000000;
    mob.heal();
    k.registerMonster(mob);
    var g = k.getMapInstance(实例地图[1]);
    g.spawnMonsterOnGroundBelow(mob, new java.awt.Point(-306, 85));
    mobId = BOSS_2_id;
    mob = em.getMonster(mobId);
    mob.setOverrideStats();
    mob.getChangedStats().hp = d;
    mob.getChangedStats().mp = 99000000;
    mob.heal();
    k.registerMonster(mob);
    var g = k.getMapInstance(实例地图[1]);
    g.spawnMonsterOnGroundBelow(mob, new java.awt.Point(358, 85));
    mobId = BOSS_3_id;
    mob = em.getMonster(mobId);
    mob.setOverrideStats();
    mob.getChangedStats().hp = l;
    mob.getChangedStats().mp = 99000000;
    mob.heal();
    k.registerMonster(mob);
    var g = k.getMapInstance(实例地图[3]);
    g.spawnMonsterOnGroundBelow(mob, new java.awt.Point(-11, 85));
    mobId = BOSS_4_id;
    mob = em.getMonster(mobId);
    mob.setOverrideStats();
    mob.getChangedStats().hp = j;
    mob.getChangedStats().mp = 99000000;
    mob.heal();
    k.registerMonster(mob);
    var g = k.getMapInstance(实例地图[5]);
    g.spawnMonsterOnGroundBelow(mob, new java.awt.Point(100, 85));
    mobId = BOSS_5_id;
    mob = em.getMonster(mobId);
    mob.setOverrideStats();
    mob.getChangedStats().hp = e;
    mob.getChangedStats().mp = 99000000;
    mob.heal();
    k.registerMonster(mob);
    var g = k.getMapInstance(实例地图[7]);
    g.spawnMonsterOnGroundBelow(mob, new java.awt.Point(128, 218));
    return k
}

function onPlayerRegistered(b, a) {
    var c = b.getMapInstance(实例地图[0]);
    a.dropMessage(6, "[" + 副本名称 + "] 进入到了挑战地图，请小心行事。");
    a.changeMap(c, c.getPortal(0));
    a.setReviveCount(复活次数);
    a.getAPI().setNumberForQuestInfo(准备地图, "damage", 0)
}

function onPlayerRevived(b, a) {
    if (a.getReviveCount() > 0) {
        return true
    } else {
        var c = b.getMapFactoryMap(准备地图);
        a.changeMap(c, c.getPortal(0));
        return true
    }
}

function onMapChanged(c, b, a) {
    if (实例地图.indexOf(a) < 0) {
        on玩家退场(c, b, false);
        return
    }
    switch (a) {
        case 实例地图[1]:
            c.schedule("Notice", 3000);
            break;
        case 实例地图[3]:
            c.schedule("Notice", 3000);
            break;
        case 实例地图[5]:
            c.schedule("Notice", 3000);
            break;
        case 实例地图[7]:
            c.schedule("Notice", 3000);
            break
    }
}

function Notice(a) {
    var f = a.getMapInstance(实例地图[1]);
    var e = a.getMapInstance(实例地图[3]);
    var c = a.getMapInstance(实例地图[5]);
    var b = a.getMapInstance(实例地图[7]);
    if (f.getCharactersSize() != 0) {
        f.getWeatherEffectNotice("为了与黑魔法师对峙，必须要消灭保护他的创造和破坏骑士。", 265, 8000, 1);
        player.getAPI().addPopupSay(0, 5000, "这里发生的攻击似乎遭到了创造或破坏的诅咒。\r\n如果同时中了两种诅咒，将会受到#b巨大伤害#k，一定要小心", "", 0);
        player.getAPI().obstacleFallRepeat(5, 75, 75, 5)
    } else {
        if (e.getCharactersSize() != 0) {
            e.getWeatherEffectNotice("终于站到黑魔法师面前了。拼尽全力消灭他吧。", 265, 8000, 1);
            player.getAPI().addPopupSay(0, 4000, "这里发生的攻击似乎遭到了创造或破坏的诅咒。\r\n如果同时中了两种诅咒，将会受到#b巨大伤害#k，一定要小心", "", 0);
            player.getAPI().obstacleFallRepeat(5, 76, 76, 5);
            addFirstMonsters(a)
        } else {
            if (c.getCharactersSize() != 0) {
                c.getWeatherEffectNotice("他似乎得到了神的权能。哪怕对方是神，也要为了大家消灭他。", 265, 8000, 1);
                player.getAPI().addPopupSay(3003902, 4000, "#face1#走吧。我去复仇，你去守护世界。", "", 0);
                player.getAPI().obstacleFallRepeat(5, 78, 78, 5);
                var d = a.getMapInstance(450013500);
                d.spawnMonsterOnGroundBelow(em.getMonster(8880509), new java.awt.Point(-831, 85));
                d.spawnMonsterOnGroundBelow(em.getMonster(8880511), new java.awt.Point(-215, 85));
                d.spawnMonsterOnGroundBelow(em.getMonster(8880511), new java.awt.Point(215, 85));
                d.spawnMonsterOnGroundBelow(em.getMonster(8880511), new java.awt.Point(810, 85))
            } else {
                if (b.getCharactersSize() != 0) {
                    b.getWeatherEffectNotice("发现了近乎于神之人的权能。是创造还是破坏，必须要选择一种力量。", 265, 3000, 1);
                    player.getAPI().obstacleFallRepeat(5, 78, 78, 5)
                }
            }
        }
    }
}

function addFirstMonsters(d) {
    var f = new java.util.Random();
    var e = f.nextInt(2) + 2;
    var b = d.getMapInstance(450013300);
    for (var c = 0; c < e; c++) {
        var a = f.nextInt(500);
        var g = 8880506;
        if (f.nextInt(100) > 50) {
            a -= 800
        }
        mob = em.getMonster(g);
        mob.setOverrideStats();
        mob.getChangedStats().hp = 300000000;
        mob.heal();
        d.registerMonster(mob);
        var b = d.getMapInstance(450013300);
        b.spawnMonsterOnGroundBelow(mob, new java.awt.Point(a, 88))
    }
    addMonsterTask = em.schedule("addFirstMonsters", 5000, d)
}

function check1(a) {
    var b = a.getMapInstance(实例地图[1]);
    b.getWeatherEffectNotice("创造与破坏的骑士倒下，通向黑魔法师之路开启。", 265, 3000, 1);
    a.getPlayers().forEach(function (c) {
        c.scheduleWarpTask(5, 实例地图[2], 0, false)
    });
    em.setProperty("state", "2")
}

function wailingwall(e) {
    if (player.getAPI().getzhizunvip() == 2) {
        var h = e.getMapInstance(实例地图[1]);
        var g = em.getNumberProperty("state");
        var d = em.getNumberProperty("stage" + g + "_kill") + 1;
        em.setProperty("stage" + g + "_kill", d);
        var a = 2400 - (d * 150);
        var f = parseInt((d + 2) % 2);
        if (f == 0) {
            var c = 8880507
        } else {
            var c = 8880508
        }
        var b = em.getMonster(c);
        if (h.getNumMonsters() < 50) {
            h.spawnMonsterOnGroundBelow(b, new java.awt.Point(a, 85));
            h.getWeatherEffectNotice("哭墙升起蚕食空间。", 265, 3000, 1)
        }
        if (Math.abs(player.getPosition().x - a) < 185) {
            e.schedule("killedWall", 50);
            player.getAPI().onTeleport(1, player.getId(), a - 200, 85);
            player.dropMessage(-1, "希纳斯精神力把你从叹息之墙中拉出来..")
        }
        if (player.getPosition().x > (a + 200)) {
            player.changeMap(h, h.getPortal(1));
            player.getAPI().onTeleport(1, player.getId(), a - 200, 85);
            player.dropMessage(-1, "希纳斯精神力把你从叹息之墙中拉出来..")
        }
        if (a < -1850) {
            e.getPlayers().forEach(function (i) {
                on玩家退场(e, i, true)
            });
            player.dropMessage(-1, "真棒！全军覆没..");
            player.dropMessage(1, "真棒！全军覆没..")
        }
        if (em.getProperty("state").equals("1")) {
            e.schedule("wailingwall", 哭墙CD * 1000)
        }
    }
}

function onMonsterKilled(b, c) {
    switch (c) {
        case BOSS血条_1_id:
            check1(b);
            break;
        case BOSS_3_id:
            var d = b.getMapInstance(实例地图[3]);
            b.getPlayers().forEach(function (e) {
                e.scheduleWarpTask(5, 实例地图[4], 0, false)
            });
            d.getWeatherEffectNotice("黑魔法师喷射出未知气息，吞噬黑暗王座。", 265, 3000, 1);
            break;
        case BOSS_4_id:
            var d = b.getMapInstance(实例地图[5]);
            b.getPlayers().forEach(function (e) {
                e.scheduleWarpTask(5, 实例地图[6], 0, false)
            });
            d.getWeatherEffectNotice("压倒性气息令周围的一切瞬间灰飞烟灭。", 265, 3000, 1);
        case BOSS_5_id:
            openNpc(b, 9130027, "评价系统");
            b.startEventTimer(退场倒计时 * 1000);
            for (var a = 0; a < b.getPlayerCount(); a++) {
                b.getPlayers().get(a).dropMessage(6, "[" + 副本名称 + "] 挑战成功！已召唤击杀奖励！！")
            }
            b.setProperty("whiteKnight", "0");
            b.setProperty("blackKnight", "0");
            break
    }
    return 1
}

function onMonsterDamaged(b, a, d, e, c) {
    if (d == BOSS_1_id || d == BOSS_2_id || d == BOSS_3_id || d == BOSS_4_id || d == BOSS_5_id) {
        a.getAPI().addNumberForQuestInfo(准备地图, "damage", e)
    }
}

function killedWall(a) {
    player.addHP(-500000);
    player.getAPI().showCashItemEffect(-500000, 0)
}

function openNpc(c, b, d) {
    for (var a = 0; a < c.getPlayerCount(); a++) {
        c.getPlayers().get(a).getAPI().openNpc(b, d)
    }
}

function randomNum(b, a) {
    switch (arguments.length) {
        case 1:
            return parseInt(Math.random() * b + 1, 10);
            break;
        case 2:
            return parseInt(Math.random() * (a - b + 1) + b, 10);
            break;
        default:
            return 0;
            break
    }
}

function initProp(a, b) {
    if (em.getProperty(a) == null) {
        em.setProperty(a, b)
    }
}

function initPropArray(a, d, c, e) {
    for (var b = c; b <= e; b++) {
        if (em.getProperty(a + "_" + b) == null) {
            em.setProperty(a + "_" + b, d)
        }
    }
}

function initPropArrayRandom(a, d, j, b, h) {
    var g = new Array();
    var k = h - b + 1;
    var c = (j - d) < (h - b) ? true : false;
    do {
        var f = randomNum(d, j);
        if (c || g.indexOf(f) < 0) {
            g.push(f);
            k--
        }
    } while (k);
    for (var e = b; e <= h; e++) {
        if (em.getProperty(a + "_" + e) == null) {
            em.setProperty(a + "_" + e, g[e - 1])
        }
    }
}

function on玩家退场(c, b, a) {
    c.disposeIfPlayerBelow(a ? 999 : 0, 准备地图);
    em.getProperties().clear();
    em.setProperty("state", 0);
    em.setProperty("leader", "true");
    c.unregisterPlayer(b)
}

function onPlayerKilled(b, a) {}

function onPartyDisbanded(a) {
    on玩家退场(a, player, false)
}

function onTimeOut(a) {
    a.getPlayers().forEach(function (b) {
        on玩家退场(a, b, true)
    })
}

function onPlayerDisconnected(b, a) {
    on玩家退场(b, a, false);
    return 0
}

function onItemPickedUp(b, a, c) {};